package com.accompany.service.impl;

import cn.hutool.core.util.StrUtil;
import com.accompany.common.core.page.TableDataInfo;
import com.accompany.common.enums.ErrorConstants;
import com.accompany.common.exception.ServiceException;
import com.accompany.domain.pojo.TbCourse;
import com.accompany.domain.dto.CoursePageDto;
import com.accompany.mapper.TbCourseMapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 33249
* @description 针对表【tb_course(课程管理)】的数据库操作Service实现
* @createDate 2025-10-10 21:52:25
*/
@Service
public class TbCourseServiceImpl extends ServiceImpl<TbCourseMapper, TbCourse>
    implements com.accompany.service.impl.TbCourseService {


    @Override
    public List<TbCourse> pageQuery(CoursePageDto coursePageDto) {

        // 分页查询
        List<TbCourse> tbCourseList = lambdaQuery()
                .like(StrUtil.isNotBlank(coursePageDto.getCode()),
                        TbCourse::getCode, coursePageDto.getCode())
                .like(StrUtil.isNotBlank(coursePageDto.getName()),
                        TbCourse::getName, coursePageDto.getName())
                .like(StrUtil.isNotBlank(coursePageDto.getApplicablePerson()),
                        TbCourse::getApplicablePerson, coursePageDto.getApplicablePerson())
                .eq(StrUtil.isNotBlank(coursePageDto.getSubject()),
                        TbCourse::getSubject, coursePageDto.getSubject())
                .orderByDesc(TbCourse::getCreateTime)
                .list();

        return tbCourseList;
    }


    @Override
    public TbCourse getInfo(Long id) {
        // 判断课程是否存在
        TbCourse tbCourse = getById(id);
        if (tbCourse == null) {
            throw new ServiceException(ErrorConstants.COURSE_IS_NOT_EXIST);
        }

        return tbCourse;
    }

    @Override
    public boolean add(TbCourse tbCourse) {
        // 判断课程名称是否重复
        if(lambdaQuery().eq(TbCourse::getName, tbCourse.getName()).one() != null) {
            throw new ServiceException(ErrorConstants.COURSE_NAME_HAS_EXIST);
        }

        tbCourse.setCreateTime(LocalDateTime.now());
        tbCourse.setUpdateTime(LocalDateTime.now());
        return save(tbCourse);
    }

    @Override
    public boolean edit(TbCourse tbCourse) {
        // 判断课程名称是否重复
        if(lambdaQuery()
                .eq(TbCourse::getName, tbCourse.getName())
                .ne(TbCourse::getId, tbCourse.getId())
                .one() != null) {
            throw new ServiceException(ErrorConstants.COURSE_NAME_HAS_EXIST);
        }

        tbCourse.setUpdateTime(LocalDateTime.now());
        return updateById(tbCourse);
    }


    @Override
    public boolean deleteByIds(List<Long> ids) {
        // 判断要删除的课程是否有不存在的
        if (lambdaQuery()
                .in(TbCourse::getId, ids)
                .count() < ids.size()) {
            throw new ServiceException(ErrorConstants.COURSE_IS_NOT_EXIST);
        }

        return lambdaUpdate()
                .set(TbCourse::getDeleted, 1)
                .in(TbCourse::getId, ids)
                .update();
    }

}




